/* @flow */

import { parse } from './parser/index'
import { optimize } from './optimizer'
import { generate } from './codegen/index'
import { createCompilerCreator } from './create-compiler'

// `createCompilerCreator` allows creating compilers that use alternative
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
// Here we just export a default compiler using the default parts.
export const createCompiler = createCompilerCreator(function baseCompile (
  template: string,
  options: CompilerOptions
): CompiledResult {
  // parse 把模板字符串转换成 抽象语法树 AST  
  // 依次遍历 html 模板字符串 
  // 把模板字符串转化成 AST 对象 并且把标签中的属性和指令都记录到 对应 AST 对象的属性中
  const ast = parse(template.trim(), options)
  if (options.optimize !== false) {
    // 优化抽象语法书
    optimize(ast, options)
  }
  // 把抽象语法书转化成字符串形式的JS代码
  const code = generate(ast, options)
  return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
})
